package niuke;
import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-04-19
 * Time: 23:25
 */

/**
 * 判断是否为平衡二叉树
 */
public class Solution7 {
        public boolean IsBalanced_Solution(TreeNode root) {
            if( root == null) return true;
            return  IsBalanced_Solution(root.left) &&
                    IsBalanced_Solution(root.right) &&
                    Math.abs( dfs( root.left) - dfs(root.right))<2;

        }

        public int dfs(TreeNode root){
            if( root == null) return 0;
            int left = dfs(root.left)+1;
            int right = dfs(root.right) +1;
            return Math.max(left, right);
        }
}
